// vim: set expandtab tabstop=4 shiftwidth=4 fdm=marker:


#ifndef _CLIB_TPL_H_
#define _CLIB_TPL_H_

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <string>
#include <map>

// version
#define CLIB_TPL_VERSTR     "0.1.1"
#define CLIB_TPL_VERNUM     101

// system define
#define CLIB_TPL_SLEN_SHOR  16
#define CLIB_TPL_SLEN_NORM  64
#define CLIB_TPL_SLEN_LONG  256
#define CLIB_TPL_SLEN_TEXT  102400

// return
#define CLIB_TPL_OK         0
#define CLIB_TPL_ERR_PARA   800
#define CLIB_TPL_ERR_READ   801
#define CLIB_TPL_ERR_FIND   802
#define CLIB_TPL_ERR_SYST   899


class clib_tpl
{
private:
    std::string mstr_filetext;
    std::string mstr_block;
    std::map<std::string, std::string> mmap_key;
    pthread_mutex_t     mst_lock;
    
public:
    /**
     * Initial clib_tpl class.
     */
    clib_tpl( void );

    
    /**
     * Destory clib_tpl class.
     */
    ~clib_tpl( void );


    /**
     * Read file content.
     *
     * @param   as_filename     Template filename.
     * @return                  0: succ,!=0: fail.
     */
    int read_file( char *as_filename );


    /**
     * Set block.
     *
     * @param   as_beg      Block begin text.
     * @param   as_end      Block end text.
     * @return              0: succ,!=0: fail.
     */
    int set_block( char *as_beg, char *as_end );


    /**
     * Set key value.
     *
     * @param   as_key      Key name.
     * @param   as_val      Value.
     * @return              0: succ,!=0: fail.
     */
    int set_key( char *as_key, char *as_val );


    /**
     * Reset key-value map.
     */
    int reset_key( void );


    /**
     * Put out block & replace key as value.
     *
     * @retval  string      Replaced block text.
     */
    std::string get_block( void );


    /**
     * Printf block & replace key as value.
     *
     * @return              0: succ,!=0: fail.
     */
    int put_block( void );


}; // class clib_tpl


#endif


